package com.example.appxi;

import java.util.StringTokenizer;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends ParentActivity {

	
	int Hot=20;

	
	
	
    @SuppressLint("NewApi")
	@Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState); 
    	setContentView(R.layout.fragment_main);
    	
    	
    	
    	//서버에 요청하는 구간
    	
    	StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
    	
    	final String[] rows = HttpRequest.get("http://dev.ts.hs.kr/trend/get_places.php").body().split(",");	
    	String[] hot_places_rows = HttpRequest.get("http://dev.ts.hs.kr/trend/get_hot_places.php").body().split("\n");
    	
    	//서버에 요청하는 구간

    	View recently_visit = (View) findViewById(R.id.recently_visit);
    	
    	 recently_visit.setOnClickListener(new OnClickListener() {
	        	public void onClick(View view){        		
	        		Intent intent;
	        		intent = new Intent(top, TransitionActivity.class);
	        		Bundle R_data = new Bundle();
	        		R_data.putStringArray("place_info", rows);
	        		intent.putExtras(R_data);
	            	startActivity(intent);
	        		}
 		 	} );
    	
    	
    	TextView myplacename = (TextView) findViewById(R.id.myplacename);
    	TextView myplaceadress = (TextView) findViewById(R.id.myplaceloca);
  
	    	myplacename.setText(rows[2]);
	    	myplaceadress.setText(rows[4]);
	    	
	    
    	
    	//여기서 부터는 주변추천과 실시간 핫을 만듦
    	
    	LinearLayout vertinear = (LinearLayout) findViewById(R.id.nearsuggestion);
    	LinearLayout vertirecent = (LinearLayout) findViewById(R.id.recentsuggestion);
        
    	 LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 10);
    	 LinearLayout.LayoutParams horiParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT,100);
    	 horiParams.setMargins(0, 0, 0, 10);
    	 
    	 
    	 for(int i=0;i<=1;i++){
    	 
    	 LinearLayout horinear = new LinearLayout(this);
    	 horinear.setOrientation(LinearLayout.HORIZONTAL);
    	 horinear.setLayoutParams(horiParams);
    	 vertinear.addView(horinear);
    	 Log.e("log", "test1");
    	 
	    	 for(int y=1; y<=4;y++){
	    		 LinearLayout button= new LinearLayout(this);
	    		 
	    		 final String places_id = rows[0];
    		 
    		 button.setId(100+4*i+y);
    		 button.setBackgroundResource(R.drawable.selector_xml_tab);
    		 button.setClickable(true);
    		 button.setLayoutParams(buttonParams);
    		 button.setOrientation(LinearLayout.VERTICAL);
    		 button.setGravity(Gravity.CENTER);
    		 button.setOnClickListener(new OnClickListener() {
 	        	public void onClick(View view){        		
 	        		Intent intent;
 	        		intent = new Intent(top, TransitionActivity.class);
	        		Bundle R_data = new Bundle();
	        		R_data.putStringArray("place_info", rows);
	        		intent.putExtras(R_data);
 	            	startActivity(intent);
 	        		}
    		 	} );
    		 
    		 TextView placename = new TextView(this);
    		 placename.setText(rows[1]);
    		 placename.setTextSize(15);
    		 placename.setGravity(Gravity.CENTER);
    		 
    		 TextView placeloca = new TextView(this);
    		 placeloca.setText(rows[3]);
    		 placeloca.setTextSize(8);
    		 placeloca.setTextColor(0xFF969696);
    		 placeloca.setGravity(Gravity.CENTER);
    		 
    		 
    		 
    		 button.addView(placename);
    		 button.addView(placeloca);
    		 
    		 horinear.addView(button);
    		 
	    	 }
    		 
    	 }

    	 for(int i=0;i<=1;i++){
        	 
        	 LinearLayout horirecent = new LinearLayout(this);
        	 horirecent.setOrientation(LinearLayout.HORIZONTAL);
        	 horirecent.setLayoutParams(horiParams);
        	 vertirecent.addView(horirecent);

        	 for(int y=0; y<=3;y++){
        		 LinearLayout button= new LinearLayout(this);
        		  		 
        		 final String[] hot_places = hot_places_rows[4*i+y].split(",");
 
        		 
        		 button.setId(100+4*i+y);
        		 button.setBackgroundResource(R.drawable.selector_xml_tab);
        		 button.setClickable(true);
        		 button.setLayoutParams(buttonParams);
        		 button.setOrientation(LinearLayout.VERTICAL);
        		 button.setGravity(Gravity.CENTER);
        		 button.setOnClickListener(new OnClickListener() {
        	        	public void onClick(View view){       
        	        		Intent intent;
        	        		intent = new Intent(top, TransitionActivity.class);
        	        		Bundle R_data = new Bundle();
        	        		R_data.putStringArray("place_info", hot_places);
        	        		intent.putExtras(R_data);
        	        		startActivity(intent);
        	        	}
        	        } );
        		 		 
        		 TextView placename = new TextView(this);
        		 placename.setText(hot_places[1]);
        		 placename.setTextSize(15);
        		 placename.setGravity(Gravity.CENTER);
        		 
        		 TextView placeloca = new TextView(this);
        		 placeloca.setText(hot_places[3]);
        		 placeloca.setTextSize(8);
        		 placeloca.setTextColor(0xFF969696);
        		 placeloca.setGravity(Gravity.CENTER);
        		 
        		 
        		 
        		 button.addView(placename);
        		 button.addView(placeloca);
        		 
        		 horirecent.addView(button);
        		 Log.e("log", "test4");
        	 	 }
        		 
        	 }
    	 
  	 
  
        top=this;

    }

 
   

    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    
   
    
    /**
     * A placeholder fragment containing a simple view.
     */
//    public static class PlaceholderFragment extends Fragment {
//
//        public PlaceholderFragment() {
//        }
//
//        @Override
//        public View onCreateView(LayoutInflater inflater, ViewGroup container,
//                Bundle savedInstanceState) {
//            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//            
//            MainActivity.btn_search = (Button)rootView.findViewById(R.id.btn_search);
//
//            MA.finishSet();
//            return rootView;
//        }
//    }

}
